package cn.rongdao.jrkabao.ui.cashadvance.uicomponent;

import android.content.Context;
import android.support.annotation.Nullable;
import android.text.Editable;
import android.util.AttributeSet;
import android.view.View;
import android.widget.LinearLayout;

import com.mifengkong.frtools.http.request.callback.FRBaseObserver;
import com.mifengkong.frtools.util.FRCollection;
import com.mifengkong.frtools.util.FRDeviceUtil;
import com.mifengkong.frtools.util.FRString;
import com.mifengkong.frtools.util.FRToast;

import cn.mifengkong.appcommon.smsverify.SMSVerify;
import cn.rongdao.jrkabao.R;
import cn.rongdao.jrkabao.model.cashadvance.BindPayGetValidateCodeModel;
import cn.rongdao.jrkabao.model.cashadvance.HeLiBaoFirstDebitModel;
import cn.rongdao.jrkabao.ui.cashadvance.CashGoPayActivity;
import cn.rongdao.jrkabao.util.CommonUtil;
import cn.rongdao.jrkabao.util.growingio.BuriedUtil;
import cn.rongdao.jrkabao.util.listener.OnTextChangeListener;
import cn.rongdao.jrkabao.widget.DialogCreditCard;

/**
 * Created by jack on 2017/11/4
 */

public class CashGoPayCreditCardInfoUIComponent extends LinearLayout {
    public CashGoPayCreditCardInfoUIComponent(Context context) {
        super(context);
    }

    public CashGoPayCreditCardInfoUIComponent(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
    }

    public CashGoPayCreditCardInfoUIComponent(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    private TextWithInputUIComponent cvnInputUIComponent;
    private TextWithInputUIComponent termOfValidityInputUIComponent;
    private TextWithInputUIComponent phoneInputUIComponent;
    private TextWithInputUIComponent verifyInputUIComponent;

    private DialogCreditCard dialogCreditCard;

    private boolean isCvnInput;
    private boolean isTermOfValidityInput;
    private boolean isVerifyInput;
    private OnNextButtonStyleListener onNextButtonStyleListener;
    private OnSendShortMsgListener onSendShortMsgListener;

    private SMSVerify smsVerify;
    private String bindId;
    private String orderNum;
    private boolean isJiFen;


    @Override
    protected void onFinishInflate() {
        super.onFinishInflate();
        cvnInputUIComponent = (TextWithInputUIComponent) findViewById(R.id.lcgci_cvn_input);
        termOfValidityInputUIComponent = (TextWithInputUIComponent) findViewById(R.id.lcgci_term_of_validity_input);
        phoneInputUIComponent = (TextWithInputUIComponent) findViewById(R.id.lcgci_phone_input);
        verifyInputUIComponent = (TextWithInputUIComponent) findViewById(R.id.lcgci_verify_input);

        cvnInputUIComponent.setInputHint("卡背面末三位数字");
        termOfValidityInputUIComponent.setInputHint("卡正面有效期，如06/21，输入0621");
        phoneInputUIComponent.setInputEnable(false);
        verifyInputUIComponent.setInputHint("请输入验证码");

        cvnInputUIComponent.setName("CVN2：");
        cvnInputUIComponent.setRightName("示例");
        termOfValidityInputUIComponent.setName("有效期：");
        phoneInputUIComponent.setName("手机号：");
        verifyInputUIComponent.setName("验证码：");
        verifyInputUIComponent.setRightName("获取验证码");
        verifyInputUIComponent.setVerifyStyle();

        cvnInputUIComponent.setInputMaxLength(3);
        termOfValidityInputUIComponent.setInputMaxLength(4);
        phoneInputUIComponent.setInputMaxLength(11);
        verifyInputUIComponent.setInputMaxLength(6);
        smsVerify = new SMSVerify(60);
        initListener();
    }

    private void initListener() {
        cvnInputUIComponent.setOnRightClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                showDialog();
            }
        });

        verifyInputUIComponent.setOnRightClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                if (isCreditInfoCorrect()) {
                    getShortMessage();
                }
            }
        });

        cvnInputUIComponent.setOnInputListener(new OnTextChangeListener() {
            @Override
            public void afterTextChanged(Editable s) {
                isCvnInput = !FRString.isEmpty(s.toString());
                if (isCvnInput) {
                    BuriedUtil.kamao_zhifu_anniu_apply(getContext(), 0);
                }
                setNextButtonStyle();
            }
        });

        termOfValidityInputUIComponent.setOnInputListener(new OnTextChangeListener() {
            @Override
            public void afterTextChanged(Editable s) {
                isTermOfValidityInput = !FRString.isEmpty(s.toString());
                if (isTermOfValidityInput) {
                    BuriedUtil.kamao_zhifu_anniu_apply(getContext(), 1);
                }
                setNextButtonStyle();
            }
        });

        verifyInputUIComponent.setOnInputListener(new OnTextChangeListener() {
            @Override
            public void afterTextChanged(Editable s) {
                isVerifyInput = !FRString.isEmpty(s.toString());
                if (isVerifyInput) {
                    BuriedUtil.kamao_zhifu_anniu_apply(getContext(), 2);
                }
                setNextButtonStyle();
            }
        });

        smsVerify.setOnSMSTimeListener(new SMSVerify.OnSMSTimeListener() {
            @Override
            public void onReceiveSMS(String second) {
                verifyInputUIComponent.setRightName(FRCollection.appendAsString(second, "S", ""));
            }

            @Override
            public void onNotReceiveSMS() {
                verifyInputUIComponent.setRightName("重新获取");
                verifyInputUIComponent.setRightEnable(true);
            }
        });
    }

    private void getShortMessage() {
        verifyInputUIComponent.setRightEnable(false);
        if (!CommonUtil.isStatusSuccess(bindId)) {
            //合力宝第一次
            HeLiBaoFirstDebitModel.getInstance().execute(isJiFen, orderNum, cvnInputUIComponent.getInputContent()
                    , termOfValidityInputUIComponent.getInputContent(), FRDeviceUtil.getDeviceId(getContext())
                    , new FRBaseObserver<String>((CashGoPayActivity) getContext(), "发送中...") {
                        @Override
                        protected void onBaseNext(String data) {
                            startCountDown();
                        }

                        @Override
                        protected void onBaseError(Throwable t) {
                            super.onBaseError(t);
                            verifyInputUIComponent.setRightEnable(true);
                        }
                    });
        } else {
            BindPayGetValidateCodeModel.getInstance().execute(isJiFen, orderNum, bindId
                    , new FRBaseObserver<String>((CashGoPayActivity) getContext(), "发送中...") {
                        @Override
                        protected void onBaseNext(String data) {
                            startCountDown();
                        }

                        @Override
                        protected void onBaseError(Throwable t) {
                            super.onBaseError(t);
                            verifyInputUIComponent.setRightEnable(true);
                        }
                    });
        }
    }

    private void startCountDown() {
        FRToast.showToastSafe("发送成功");
        smsVerify.startCountDown();
        if (null != onSendShortMsgListener) {
            onSendShortMsgListener.onSendMsg(true);
        }
    }

    private void setNextButtonStyle() {
        if (null != onNextButtonStyleListener) {
            onNextButtonStyleListener.onNextButton(isCvnInput && isTermOfValidityInput && isVerifyInput);
        }
    }

    private void showDialog() {
        if (null == dialogCreditCard) {
            DialogCreditCard.Builder builder = new DialogCreditCard.Builder(getContext());
            dialogCreditCard = builder.create();
        }
        dialogCreditCard.show();
    }

    public void setPhoneContent(String phone) {
        phoneInputUIComponent.setInputContent(phone);
    }

    public void setBindId(String bindId, String orderNum, boolean isJiFen) {
        this.bindId = bindId;
        this.orderNum = orderNum;
        this.isJiFen = isJiFen;
    }

    private boolean isCreditInfoCorrect() {
        if (FRString.isEmpty(cvnInputUIComponent.getInputContent()) || cvnInputUIComponent.getInputContent().length() < 3) {
            FRToast.showToastSafe("请填写正确的CVN2信息");
            return false;
        }
        if (FRString.isEmpty(termOfValidityInputUIComponent.getInputContent()) || termOfValidityInputUIComponent.getInputContent().length() < 4) {
            FRToast.showToastSafe("请填写正确的有效期");
            return false;
        }
        return true;
    }

    public String getCvn2() {
        return cvnInputUIComponent.getInputContent();
    }

    public String getTermOfValidity() {
        return termOfValidityInputUIComponent.getInputContent();
    }

    public String getVerifyDataMsg() {
        return verifyInputUIComponent.getInputContent();
    }

    public boolean isInputCorrect() {
        if (!isCreditInfoCorrect()) {
            return false;
        }
        if (FRString.isEmpty(verifyInputUIComponent.getInputContent()) || verifyInputUIComponent.getInputContent().length() < 6) {
            FRToast.showToastSafe("请填写正确的验证码");
            return false;
        }
        return true;
    }

    public interface OnSendShortMsgListener {
        void onSendMsg(boolean isSendMsg);
    }

    public void setOnSendShortMsgListener(OnSendShortMsgListener onSendShortMsgListener) {
        this.onSendShortMsgListener = onSendShortMsgListener;
    }

    public void setOnNextButtonStyleListener(OnNextButtonStyleListener onNextButtonStyleListener) {
        this.onNextButtonStyleListener = onNextButtonStyleListener;
    }

    public interface OnNextButtonStyleListener {
        void onNextButton(boolean isInput);
    }
}